import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
import VueCookie from "vue-cookies";
import Voluntee from "../views/admin/Voluntee";
import Forewarning from "../views/admin/forewarning";
// import HomeView from '../views/HomeView.vue'
// import addAttendance from "../views/admin/CheckAttendance/addAttendance";
import CheckAttendance from "../views/admin/CheckAttendance";
import Login from "../views/login";
import layout from "../layout";
const originalPush = VueRouter.prototype.push;
import { flterSyncRouter } from "@/utils/permisson.js";
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
const originalReplace = VueRouter.prototype.replace;

VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch((err) => err);
};
Vue.use(VueRouter);
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Login",
    component: Login,
    redirect: "/login",
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/about",
    name: "about",
    component: () =>
      import(/* webpackChunkName: "about" */ "@/views/AboutView.vue"),
  },
  // 公安系统
  {
    path: "/layout",
    name: "layout",
    component: layout,
    redirect: "/layout/imgWrite",
    children: [
      {
        path: "warn",
        name: "Warn",
        component: () => import("@/views/pages/warn"),
      },
      {
        path: "imgwrite",
        name: "imgWrite",
        component: () => import("@/views/pages/imgWrite"),
      },
      {
        path: "visitor",
        name: "Visitor",
        component: () => import("@/views/pages/visitor"),
      },
      {
        path: "UnitManagement",
        name: "UnitManagement",
        component: () => import("@/views/pages/UnitManagement"),
      },
      {
        path: "punchLog",
        name: "PunchLog",
        component: () => import("@/views/pages/punchLog"),
      },
      {
        path: "voluntee",
        name: "Voluntee",
        component: () => import("@/views/pages/Voluntee"),
      },
      // {
      //   path: "permisson",
      //   name: "Permisson",
      //   component: () => import("@/views/pages/permisson/PersonnelManagement"),
      //   children: [
      //     {
      //       path: "role",
      //       name: "Role",
      //       component: () => import("@/views/pages/permisson/role"),
      //     },
      //     {
      //       path: "menu",
      //       name: "Menu",
      //       component: () => import("@/views/pages/permisson/menu"),
      //     },
      //     {
      //       path: "partment",
      //       name: "Partment",
      //       component: () => import("@/views/pages/permisson/partment"),
      //     },
      //     {
      //       path: "PersonnelManagement",
      //       name: "PersonnelManagement",
      //       component: () =>
      //         import("@/views/pages/permisson/PersonnelManagement"),
      //     },
      //   ],
      // },
    ],
  },
  // 管理员
  {
    path: "/admin",
    name: "Admin",
    component: layout,
    redirect: "/admin/admin",
    children: [
      {
        path: "admin",
        name: "admin",
        component: () => import("../views/admin/schoolInfo"),
      },
      {
        path: "attendanceRate",
        name: "AttendanceRate",
        component: () => import("../views/admin/attendanceRate"),
      },
      {
        path: "imgWrite",
        name: "ImgWrite",
        component: () => import("../views/admin/imgWrite"),
      },
      {
        path: "warn",
        name: "Warn",
        component: () => import("../views/admin/warn"),
      },

      {
        path: "punchLogSchool",
        name: "punchLogSchool",
        component: () => import("../views/admin/punchLogSchool"),
      },
      // SchoolNotice
      {
        path: "SchoolNotice",
        name: "SchoolNotice",
        component: () => import("../views/admin/SchoolNotice"),
      },
      {
        path: "visitor",
        name: "Visitor",
        component: () => import("../views/admin/visitor"),
      },
      {
        path: "dglsjtj",
        name: "Dglsjtj",
        component: () => import("../views/admin/dglsjtj"),
      },
      {
        path: "voluntee",
        name: "Voluntee",
        component: Voluntee,
        children: [
          {
            path: "Voluntee",
            name: "Voluntee",
            component: () => import("@/views/admin/Voluntee/voluntee/index"),
          },
          {
            path: "teacher",
            name: "teacher",
            component: () => import("@/views/admin/Voluntee/teacher/index"),
          },
          {
            path: "anbao",
            name: "anbao",
            component: () => import("@/views/admin/Voluntee/anbao/index"),
          },
        ],
      },
      {
        path: "forewarning",
        name: "forewarning",
        component: Forewarning,
        redirect: "/admin/forewarning/peopleWarning",
        children: [
          {
            path: "peopleWarning",
            name: "peopleWarning",
            component: () =>
              import("@/views/admin/forewarning/peopleWarning/index"),
          },
          {
            path: "carWarning",
            name: "carWarning",
            component: () =>
              import("@/views/admin/forewarning/carWarning/index"),
          },
        ],
      },
      {
        path: "CheckAttendance",
        name: "CheckAttendance",
        meta: {
          title: "考勤管理",
        },
        component: CheckAttendance,
        children: [
          {
            path: "banci",
            name: "banci",
            component: () => import("@/views/admin/CheckAttendance/banci"),
          },
          {
            path: "kqpz",
            name: "kqpz",
            component: () => import("@/views/admin/CheckAttendance/kqpz"),
          },
          {
            path: "addAttendance",
            name: "addAttendance",
            component: () =>
              import("@/views/admin/CheckAttendance/kqpz/addAttendance.vue"),
          },
        ],
      },
      {
        path: "LeaveApproval",
        name: "LeaveApproval",
        meta: {
          title: "请假信息管理",
        },
        component: () => import("../views/admin/LeaveApproval"),
      },
      {
        path: "devicenformation",
        name: "Devicenformation",
        component: () => import("../views/admin/Devicenformation"),
      },
      {
        path: "basicInformation",
        name: "BasicInformation",
        component: () => import("../views/admin/BasicInformation"),
      },
      {
        path: "punchLog",
        name: "PunchLog",
        component: () => import("@/views/admin/punchLog"),
      },
      {
        path: "dataDoard",
        name: "DataDoard",
        component: () => import("@/views/admin/dataDoard"),
      },
      {
        path: "permisson",
        name: "Permisson",
        component: () => import("@/views/admin/permisson"),
        children: [
          {
            path: "role",
            name: "Role",
            component: () => import("@/views/admin/permisson/role"),
          },
          {
            path: "menu",
            name: "Menu",
            component: () => import("@/views/admin/permisson/menu"),
          },
          // {
          //   path: "partment",
          //   name: "Partment",
          //   component: () => import("@/views/admin/permisson/partment"),
          // },
          {
            path: "PersonnelManagement",
            name: "PersonnelManagement",
            component: () =>
              import("@/views/admin/permisson/PersonnelManagement"),
          },
        ],
      },
    ],
  },
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});
// const whiteList = ["Login"];
router.beforeEach((to, from, next) => {
  const token = VueCookie.get("token"); // 有问题
  // console.log(routes[4]);
  // console.log(token);
  // console.log(to);
  next();
  // if (token) {
  //   if (whiteList.includes(to.name)) {
  //     // 判断去的是否是Login页面  有token 并且在白名单里 允许通过
  //     next("/");
  //   } else {
  //     // loadMenus(to, next)
  //     // vuex 中存储的值时候存在
  //     if (!store.state.menuList.length) {
  //       loadMenus(to, next);
  //     } else {
  //       next();
  //     }
  //   }
  // } else {
  //   store.commit("SET_MENULIST", [4, 5, 6]);
  //   localStorage.removeItem("menus");
  //   VueCookie.remove("token");
  //   if (whiteList.includes(to.name)) {
  //     next();
  //   } else {
  //     next("/login");
  //   }
  // }
});

//  解析menus
function loadMenus(to, next) {
  // 获取路由表
  const menus = localStorage.getItem("menus")
    ? JSON.parse(localStorage.getItem("menus"))
    : [];
  // console.log(menus)
  // 路由解析
  const syncRouter = flterSyncRouter(menus);
  // console.log(syncRouter)
  //  要想vux中 存储路由
  store.commit("SET_MENULIST", syncRouter);
  // 路由合并
  const xRouter = [...routes, ...syncRouter];
  router.matcher = new VueRouter().matcher;
  // 添加新路由
  // router.addRoutes(xRouter)
  xRouter.forEach((item) => {
    router.addRoute(item);
  });
  next({ ...to });
}
// function generateRoutes(a){
//   const routers = []
//   if(a)[]
// }
export default router;
